package javolution.xml.stream;

import java.util.Iterator;
import javax.realtime.MemoryArea;
import javolution.lang.Reusable;
import javolution.text.CharArray;
import javolution.util.FastList;

/* loaded from: classes2.dex */
final class NamespacesImpl implements Reusable, NamespaceContext {
    static final int NBR_PREDEFINED_NAMESPACES = 3;
    CharArray _defaultNamespace;
    int _defaultNamespaceIndex;
    CharArray[] _namespaces;
    int[] _namespacesCount;
    private CharArray[] _namespacesTmp;
    private CharArray[] _prefixesTmp;
    boolean[] _prefixesWritten;
    final CharArray _nullNsURI = new CharArray("");
    final CharArray _defaultNsPrefix = new CharArray("");
    final CharArray _xml = new CharArray("xml");
    final CharArray _xmlURI = new CharArray("http://www.w3.org/XML/1998/namespace");
    final CharArray _xmlns = new CharArray("xmlns");
    final CharArray _xmlnsURI = new CharArray("http://www.w3.org/2000/xmlns/");
    private int _nesting = 0;
    CharArray[] _prefixes = new CharArray[16];

    public NamespacesImpl() {
        CharArray[] charArrayArr = this._prefixes;
        this._namespaces = new CharArray[charArrayArr.length];
        this._prefixesWritten = new boolean[charArrayArr.length];
        this._namespacesCount = new int[16];
        CharArray charArray = this._nullNsURI;
        this._defaultNamespace = charArray;
        this._prefixesTmp = new CharArray[charArrayArr.length];
        this._namespacesTmp = new CharArray[charArrayArr.length];
        charArrayArr[0] = this._defaultNsPrefix;
        CharArray[] charArrayArr2 = this._namespaces;
        charArrayArr2[0] = charArray;
        charArrayArr[1] = this._xml;
        charArrayArr2[1] = this._xmlURI;
        charArrayArr[2] = this._xmlns;
        charArrayArr2[2] = this._xmlnsURI;
        this._namespacesCount[0] = 3;
    }

    private void resizeNamespacesCount() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.xml.stream.NamespacesImpl.3
            @Override // java.lang.Runnable
            public void run() {
                int length = NamespacesImpl.this._namespacesCount.length;
                int[] iArr = new int[length * 2];
                System.arraycopy(NamespacesImpl.this._namespacesCount, 0, iArr, 0, length);
                NamespacesImpl.this._namespacesCount = iArr;
            }
        });
    }

    private void resizePrefixStack() {
        MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.xml.stream.NamespacesImpl.4
            @Override // java.lang.Runnable
            public void run() {
                int length = NamespacesImpl.this._prefixes.length;
                int i = length * 2;
                CharArray[] charArrayArr = new CharArray[i];
                System.arraycopy(NamespacesImpl.this._prefixes, 0, charArrayArr, 0, length);
                NamespacesImpl namespacesImpl = NamespacesImpl.this;
                namespacesImpl._prefixes = charArrayArr;
                CharArray[] charArrayArr2 = new CharArray[i];
                System.arraycopy(namespacesImpl._namespaces, 0, charArrayArr2, 0, length);
                NamespacesImpl namespacesImpl2 = NamespacesImpl.this;
                namespacesImpl2._namespaces = charArrayArr2;
                boolean[] zArr = new boolean[i];
                System.arraycopy(namespacesImpl2._prefixesWritten, 0, zArr, 0, length);
                NamespacesImpl namespacesImpl3 = NamespacesImpl.this;
                namespacesImpl3._prefixesWritten = zArr;
                CharArray[] charArrayArr3 = new CharArray[i];
                System.arraycopy(namespacesImpl3._prefixesTmp, 0, charArrayArr3, 0, length);
                NamespacesImpl.this._prefixesTmp = charArrayArr3;
                CharArray[] charArrayArr4 = new CharArray[i];
                System.arraycopy(NamespacesImpl.this._namespacesTmp, 0, charArrayArr4, 0, length);
                NamespacesImpl.this._namespacesTmp = charArrayArr4;
            }
        });
    }

    private void searchDefaultNamespace() {
        int i = this._namespacesCount[this._nesting];
        do {
            i--;
            if (i < 0) {
                throw new Error("Cannot find default namespace");
            }
        } while (this._prefixes[i].length() != 0);
        this._defaultNamespaceIndex = i;
    }

    @Override // javolution.xml.stream.NamespaceContext
    public CharArray getNamespaceURI(CharSequence charSequence) {
        if (charSequence != null) {
            return getNamespaceURINullAllowed(charSequence);
        }
        throw new IllegalArgumentException("null prefix not allowed");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public CharArray getNamespaceURINullAllowed(CharSequence charSequence) {
        if (charSequence == null || charSequence.length() == 0) {
            return this._defaultNamespace;
        }
        int i = this._namespacesCount[this._nesting];
        do {
            i--;
            if (i < 0) {
                return null;
            }
        } while (!this._prefixes[i].equals((Object) charSequence));
        return this._namespaces[i];
    }

    @Override // javolution.xml.stream.NamespaceContext
    public CharArray getPrefix(CharSequence charSequence) {
        if (charSequence != null) {
            return this._defaultNamespace.equals((Object) charSequence) ? this._defaultNsPrefix : getPrefix(charSequence, this._namespacesCount[this._nesting]);
        }
        throw new IllegalArgumentException("null namespace URI not allowed");
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public CharArray getPrefix(CharSequence charSequence, int i) {
        int i2 = i;
        while (true) {
            i2--;
            if (i2 < 0) {
                return null;
            }
            CharArray charArray = this._prefixes[i2];
            if (this._namespaces[i2].equals((Object) charSequence)) {
                boolean z = false;
                int i3 = i2 + 1;
                while (true) {
                    if (i3 >= i) {
                        break;
                    } else if (charArray.equals(this._prefixes[i3])) {
                        z = true;
                        break;
                    } else {
                        i3++;
                    }
                }
                if (!z) {
                    return charArray;
                }
            }
        }
    }

    @Override // javolution.xml.stream.NamespaceContext
    public Iterator getPrefixes(CharSequence charSequence) {
        FastList fastList = new FastList();
        int i = this._namespacesCount[this._nesting];
        while (true) {
            i--;
            if (i < 0) {
                return fastList.iterator();
            }
            if (this._namespaces[i].equals((Object) charSequence)) {
                fastList.add(this._prefixes[i]);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void pop() {
        int[] iArr = this._namespacesCount;
        int i = this._nesting - 1;
        this._nesting = i;
        if (iArr[i] <= this._defaultNamespaceIndex) {
            searchDefaultNamespace();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void push() {
        this._nesting++;
        if (this._nesting >= this._namespacesCount.length) {
            resizeNamespacesCount();
        }
        int[] iArr = this._namespacesCount;
        int i = this._nesting;
        iArr[i] = iArr[i - 1];
    }

    @Override // javolution.lang.Reusable
    public void reset() {
        this._defaultNamespace = this._nullNsURI;
        this._defaultNamespaceIndex = 0;
        this._namespacesCount[0] = 3;
        this._nesting = 0;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void setPrefix(CharSequence charSequence, CharSequence charSequence2, boolean z) {
        final int i = this._namespacesCount[this._nesting];
        this._prefixesWritten[i] = z;
        final int length = charSequence.length();
        CharArray charArray = this._prefixesTmp[i];
        if (charArray == null || charArray.array().length < length) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.xml.stream.NamespacesImpl.1
                @Override // java.lang.Runnable
                public void run() {
                    NamespacesImpl.this._prefixesTmp[i] = new CharArray().setArray(new char[length + 32], 0, 0);
                }
            });
            charArray = this._prefixesTmp[i];
        }
        for (int i2 = 0; i2 < length; i2++) {
            charArray.array()[i2] = charSequence.charAt(i2);
        }
        charArray.setArray(charArray.array(), 0, length);
        final int length2 = charSequence2.length();
        CharArray charArray2 = this._namespacesTmp[i];
        if (charArray2 == null || charArray2.array().length < length2) {
            MemoryArea.getMemoryArea(this).executeInArea(new Runnable() { // from class: javolution.xml.stream.NamespacesImpl.2
                @Override // java.lang.Runnable
                public void run() {
                    NamespacesImpl.this._namespacesTmp[i] = new CharArray().setArray(new char[length2 + 32], 0, 0);
                }
            });
            charArray2 = this._namespacesTmp[i];
        }
        for (int i3 = 0; i3 < length2; i3++) {
            charArray2.array()[i3] = charSequence2.charAt(i3);
        }
        charArray2.setArray(charArray2.array(), 0, length2);
        setPrefix(charArray, charArray2);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void setPrefix(CharArray charArray, CharArray charArray2) {
        int i = this._namespacesCount[this._nesting];
        this._prefixes[i] = charArray;
        this._namespaces[i] = charArray2;
        if (charArray.length() == 0) {
            this._defaultNamespaceIndex = i;
            this._defaultNamespace = charArray2;
        }
        int[] iArr = this._namespacesCount;
        int i2 = this._nesting;
        int i3 = iArr[i2] + 1;
        iArr[i2] = i3;
        if (i3 >= this._prefixes.length) {
            resizePrefixStack();
        }
    }
}
